package com.zzptc.zzsgz.bdsjws.fragments;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.zzptc.zzsgz.bdsjws.MyApplication;
import com.zzptc.zzsgz.bdsjws.R;
import com.zzptc.zzsgz.bdsjws.activities.AddContactsActivity;
import com.zzptc.zzsgz.bdsjws.activities.OneKeySOSActivity;
import com.zzptc.zzsgz.bdsjws.adapter.SOSAdapter;
import com.zzptc.zzsgz.bdsjws.com.Constants;
import com.zzptc.zzsgz.bdsjws.fragments.base.BaseFragmentbase;
import com.zzptc.zzsgz.bdsjws.utils.MyUtil;
import com.zzptc.zzsgz.bdsjws.vo.Contact;

import org.w3c.dom.Text;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by zzsgz on 2017/5/27.
 */

public class SecondFragment extends BaseFragmentbase {


    @BindView(R.id.et_add)
    EditText etAdd;
    @BindView(R.id.img_add)
    ImageView imgAdd;
    @BindView(R.id.btn_yes)
    Button btnYes;

    @BindView(R.id.rvSooTel)
    RecyclerView rvSooTel;
//    @BindView(R.id.tv_num)
//    TextView tv_num;
    SOSAdapter sosAdapter;
    @Override
    public void initView() {
        etAdd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                btnYes.setVisibility(View.VISIBLE);
                imgAdd.setVisibility(View.GONE);
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

    }

    @Override
    public void initData() {

    }

    @Override
    public int getXML() {
        return R.layout.second_fragment;

    }


    @OnClick(R.id.img_add)
    public void onViewClicked() {
        //Toast.makeText(getActivity(), "======", Toast.LENGTH_SHORT).show();
        startActivity(new Intent(getActivity(), AddContactsActivity.class));
    }



    @OnClick(R.id.btn_yes)
    public void onYesClicked() {
        //先判断电话号码的长度  并且要获取电话号码
        String tel=etAdd.getText().toString().trim();
        if(tel.length()==11&&tel.matches("^1[3578]\\d{9}$")){
            //放入集合中
            List<Contact> list= MyApplication.getSoslist();
            Contact contact = new Contact();
            contact.setPhonenum(tel);
            if (list.size()<3){
                list.add(contact);
            }

            sosAdapter = new SOSAdapter(list);
            rvSooTel.setLayoutManager(new LinearLayoutManager(getActivity()));
            rvSooTel.setAdapter(sosAdapter);
        }

        //判断电话号码的长度
        etAdd.setText("");
        btnYes.setVisibility(View.GONE);
        imgAdd.setVisibility(View.VISIBLE);

    }

    @OnClick(R.id.btn_success)
    public void tothreeFragment(){
//        Toast.makeText(getActivity(),"ok",Toast.LENGTH_SHORT).show();
        SharedPreferences sharedPreferences= MyUtil.getSharedPre();
//        String info = sharedPreferences.getString("sos",null);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("sos","SOS!!!--------SOS!!!");
        editor.commit();
        //将求救电话放入数据库
            for (int i=0;i<MyApplication.getSoslist().size();i++){
                Contact contact=MyApplication.getSoslist().get(i);
//                if (MyUtil.getAllContacts().contains(contact.getPhonenum()){
//
//
//                }
            }

        if (MyApplication.getSoslist().size()==0){
            Toast.makeText(getActivity(),"请选择求救联系人",Toast.LENGTH_SHORT).show();
        }else {
            if (getActivity() instanceof OneKeySOSActivity){
                ((OneKeySOSActivity) getActivity()).toThirdFragment();
            }
        }
    }



    @Override
    public void onResume() {
        super.onResume();
        if (sosAdapter!=null){
            sosAdapter.notifyDataSetChanged();
        }
    }
}
